<template>
  <div class="mod-winprize">
    <el-form :inline="true" :model="searchForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="searchForm.name" clearable placeholder="用户名"></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="searchForm.userMobile" clearable placeholder="手机号"></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="searchForm.idCard" clearable placeholder="身份证后6位"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button type="warning" @click="download()">导出中奖记录</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      style="width: 100%;"
      @selection-change="selectionChangeHandle">
      <el-table-column
        align="center"
        header-align="center"
        type="selection"
        width="50">
      </el-table-column>
      <el-table-column
        align="center"
        header-align="center"
        label="用户名称"
        prop="userName">
      </el-table-column>
      <el-table-column
        align="center"
        header-align="center"
        label="手机号码"
        prop="userMobile">
      </el-table-column>
      <el-table-column
        align="center"
        header-align="center"
        label="身份证后6位"
        prop="idCard">
      </el-table-column>
      <el-table-column
        :formatter="(a,b,c)=>type[c]"
        align="center"
        header-align="center"
        label="类型"
        prop="type">
      </el-table-column>
      <el-table-column
        align="center"
        header-align="center"
        label="商品名称"
        prop="commodityName">
      </el-table-column>
      <el-table-column
        align="center"
        header-align="center"
        label="获奖时间"
        prop="addTime">
      </el-table-column>
    </el-table>
    <el-pagination
      :current-page="pageIndex"
      :page-size="pageSize"
      :page-sizes="[10, 20, 50, 100]"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
import AddOrUpdate from './winprize-add-or-update'
import {export_json_to_excel} from "../../../excel/Export2Excel";

export default {
  data() {
    return {
      searchForm: {
        name: '',
        userMobile: '',
        idCard: ''
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListSelections: [],
      addOrUpdateVisible: false,
      type: {
        1: '卡券',
        4: '其他'
      }
    }
  },
  components: {
    AddOrUpdate
  },
  activated() {
    this.getDataList()
  },
  methods: {
    // 获取数据列表
    getDataList() {
      let params = JSON.parse(JSON.stringify(this.searchForm))
      params['page'] = this.pageIndex
      params['limit'] = this.pageSize
      this.$http({
        url: '/mall/winprize/list',
        method: 'get',
        params: params
      }).then(({data}) => {
        if (data && data.code === 0) {
          this.dataList = data.page.records
          this.totalPage = data.page.total
        } else {
          this.dataList = []
          this.totalPage = 0
        }
      })
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val
    },
    //导出
    download() {
      let params = JSON.parse(JSON.stringify(this.searchForm))
      this.$http({
        url: '/mall/winprize/queryAll',
        method: 'get',
        params: params
      }).then(({data}) => {
        if (data && data.code === 0) {
          data.list.forEach((e) => {
            e.type = e.type == 1 ? "卡卷" : "其他";
          });
          const {export_json_to_excel} = require('../../../excel/Export2Excel')
          let tHeader = []
          let filterVal = []
          tHeader = ['用户名称', '手机号码', '类型', '商品名称', '获奖时间']// 对应表格输出的title
          filterVal = ['userName', 'userMobile', 'type', 'commodityName', 'addTime'] // 对应表格输出的数据
          const exceldata = this.formatJson(filterVal, data.list)
          export_json_to_excel([tHeader], [exceldata], '抽奖记录')// 对应下载文件的名字
        } else {
          this.$message({
            message: '未查找到数据',
            type: 'success',
            duration: 1500
          })
        }
      })
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => v[j]))
    },
  }
}
</script>
